package com.zhouyibike.uu.ui.activity.mywallet;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.LoopPagerAdapter;
import com.jude.rollviewpager.hintview.ColorPointHintView;
import com.squareup.picasso.NetworkPolicy;
import com.squareup.picasso.Picasso;
import com.zhouyibike.uu.R;
import com.zhouyibike.uu.entity.ProduceListResult;
import com.zhouyibike.uu.ui.activity.mybike.Imagesee.ImagePagerActivity;
import com.zhouyibike.uu.ui.activity.mybike.Imagesee.ImagePagerActivity2;
import com.zhouyibike.uu.ui.base.BaseActivity;
import com.zhouyibike.uu.utils.DataSingle;

import java.text.DecimalFormat;
import java.util.ArrayList;

public class DuihuanshangpinxiangqingActivity extends BaseActivity implements View.OnClickListener{

    private ImageButton mBtnBack;
    private TextView mBtnTitle,tv_zrnumber,tv_zrprice;
    private Button btn_gobuy;
    private TextView tv_jian,tv_jia,shangpinname,shangpinxiaoshounum,fahuoshijian;
    private EditText et_numbershow;
    private int zrnum=1;
    private double shangpinprice=0;
    private RollPagerView mRollViewPager;
    private ArrayList<String> bannerPictureURLs = new ArrayList<>();
    ProduceListResult.DataBean myshangpin;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_duihuanshangpinxiangqing);
        myshangpin= (ProduceListResult.DataBean) getIntent().getSerializableExtra("data");
        initView();
        getMsg();
    }
    private void getMsg() {
        shangpinprice=myshangpin.getPrice();
        tv_zrnumber.setText("共计"+zrnum + "件");
        tv_zrprice.setText(myshangpin.getUnit()+df.format(Double.parseDouble(shangpinprice+"") * zrnum) );
        fahuoshijian.setText(myshangpin.getIntroduce() );
        shangpinname.setText(myshangpin.getTitle() );
        shangpinxiaoshounum.setText("已售"+myshangpin.getAmount()+"件" );
        mBtnTitle.setText(""+myshangpin.getTitle());
        bannerPictureURLs.clear();
        //  mBtnTitle.setText(model.getData().getTitle());
        for (int i = 0; i < myshangpin.getBannars().size(); i++) {
           bannerPictureURLs.add(myshangpin.getBannars().get(i).getImagePath());
        }
        double scyle = 0;
        if (myshangpin.getBannars() != null && myshangpin.getBannars().size() > 0) {
            scyle = Double.parseDouble(myshangpin.getBannars().get(0).getHeight()) / Double.parseDouble(myshangpin.getBannars().get(0).getWidth());
        } else {
            scyle = 0.6284444444444445;
            //1.5912305516265912;
        }
        mRollViewPager.setLayoutParams(new AppBarLayout.LayoutParams(DataSingle.getInstance().PhoneW, (int) (DataSingle.getInstance().PhoneW * scyle)));
        mRollViewPager.setPlayDelay(5000);
        mRollViewPager.setAnimationDurtion(500);
        mRollViewPager.setAdapter(new TestLoopAdapter(mRollViewPager)); //设置适配器
        mRollViewPager.setHintView(new ColorPointHintView(DuihuanshangpinxiangqingActivity.this, getResources().getColor(R.color.radio_button_selected_color), getResources().getColor(R.color.div_total_color)));// 设置圆点指示器颜色
        // mRollViewPager.setHintView(new IconHintView(this, R.drawable.point_focus, R.drawable.point_normal));
        if (bannerPictureURLs.size()<=1)
        {
            mRollViewPager.setHintView(null);
        }
    }
    private void initView(){
        shangpinname= (TextView) findViewById(R.id.shangpinname);
        shangpinxiaoshounum= (TextView) findViewById(R.id.xiaoshounum);
        fahuoshijian= (TextView) findViewById(R.id.fahuoshijian);
        btn_gobuy= (Button) findViewById(R.id.btn_gobuy);
        mBtnBack = (ImageButton) findViewById(R.id.bar_btn_back);
        mBtnTitle = (TextView) findViewById(R.id.bar_text_title);
        mBtnBack.setOnClickListener(this);
        btn_gobuy.setOnClickListener(this);
        mRollViewPager = (RollPagerView)findViewById(R.id.roll_view_pager);
        tv_zrnumber= (TextView) findViewById(R.id.goumainum);
        tv_zrprice= (TextView) findViewById(R.id.goumaiprice);
        tv_jian= (TextView) findViewById(R.id.tv_zr_jian);
        tv_jian.setOnClickListener(this);
        tv_jia= (TextView) findViewById(R.id.tv_zr_jia);
        tv_jia.setOnClickListener(this);
        et_numbershow= (EditText) findViewById(R.id.et_zr_numshow);
        et_numbershow.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                if (et_numbershow.getText().toString().length()<1)
                {
                    return;
                }
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (et_numbershow.getText().toString().length()<1||Integer.parseInt(et_numbershow.getText().toString())<0)
                {
                    tv_jian.setEnabled(false);
                    zrnum = 0;
                    et_numbershow.setSelection(s.length());//将光标移至文字末尾
                    tv_zrnumber.setText("共计"+zrnum + "件");
                    tv_zrprice.setText(df.format(Double.parseDouble(shangpinprice+"") * zrnum) + "元");
                }else{
                    tv_jia.setEnabled(true);
                    tv_jian.setEnabled(true);
                    zrnum = Integer.parseInt(et_numbershow.getText().toString());
                    if (zrnum==0)
                    {
                        tv_jian.setEnabled(false);
                    }
                    et_numbershow.setSelection(s.length());//将光标移至文字末尾
                    tv_zrnumber.setText("共计"+zrnum + "件");
                    tv_zrprice.setText("翼券:"+df.format(Double.parseDouble(shangpinprice+"") * zrnum) );
                }
            }
        });
    }
    DecimalFormat df = new DecimalFormat("######0");
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bar_btn_back:
            case R.id.btn_txsuccess_ljyc:
                finish();
                break;
            case R.id.btn_gobuy:
                if (zrnum<=0)
                {
                    toastShow("购买数量不能小于0");
                }else{
                    Intent intent=new Intent(DuihuanshangpinxiangqingActivity.this,ShouhuorenxinxiActivity.class);
                    intent.putExtra("num",zrnum);
                    intent.putExtra("shangpin",myshangpin);
                    startActivityForResult(intent,200);
                }

                break;
            case R.id.tv_zr_jian:
                if(zrnum==0){
                    //  toastShow("不能再少了");
                    tv_jian.setEnabled(false);
                }else
                {
                    zrnum--;
                    et_numbershow.setText(zrnum+"");
                    et_numbershow.setSelection(et_numbershow.getText().toString().length());//将光标移至文字末尾
                    tv_zrnumber.setText("共计"+zrnum + "件");
                    tv_zrprice.setText("翼券:"+df.format(Double.parseDouble(shangpinprice+"") * zrnum) );
                }
                break;
            case R.id.tv_zr_jia:
//                if(zrnum==zrtotalnum){
//                    tv_jia.setEnabled(false);
//                    toastShow("不能再多了");
//                }else
            {
                zrnum++;
                et_numbershow.setText(zrnum+"");
                et_numbershow.setSelection(et_numbershow.getText().toString().length());//将光标移至文字末尾
                tv_zrnumber.setText("共计"+zrnum + "件");
                tv_zrprice.setText("翼券:"+df.format(Double.parseDouble(shangpinprice+"") * zrnum) );
            }
            break;
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==101&&requestCode==200)
        {
            finish();
        }
    }
    private class TestLoopAdapter extends LoopPagerAdapter {
        // private int[] imgs = {R.drawable.img0, R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4,};  // 本地图片
        private int count = bannerPictureURLs.size(); // banner上图片的数量

        public TestLoopAdapter(RollPagerView viewPager) {
            super(viewPager);
        }

        @Override
        public View getView(ViewGroup container, int position) {
            final int picNo = position ;
            ImageView view = new ImageView(container.getContext());
            //   view.setImageResource(imgs[position]);
            view.setScaleType(ImageView.ScaleType.FIT_XY);
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
//            Picasso.with(RengouActivity.this).load(bannerPictureURLs.get(position)).placeholder(R.drawable.zhanweitu).error(R.drawable.zhanweitu).into(view);  // 加载网络图片
            Picasso.with(DuihuanshangpinxiangqingActivity.this).load(bannerPictureURLs.get(position)).placeholder(R.drawable.zhanweitu).error(R.drawable.zhanweitu).networkPolicy(NetworkPolicy.NO_CACHE,NetworkPolicy.NO_STORE)
                    .into(view);  // 加载网络图片
            view.setOnClickListener(new View.OnClickListener()      // 点击事件
            {
                @Override
                public void onClick(View v)
                {
                    if (bannerPictureURLs!=null&&bannerPictureURLs.size()>0)
                    {
                        Intent intent = new Intent(DuihuanshangpinxiangqingActivity.this, ImagePagerActivity2.class);
                        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, (bannerPictureURLs));
                        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX,picNo);
                        startActivity(intent);
                    }}

            });

            return view;
        }

        @Override
        public int getRealCount() {
            return count;
        }

    }
}
